package net.lab1024.sa.admin.module.business.supercomputer.datatask.controller;

import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.dto.StatusDTO;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.form.DatataskAddForm;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.form.DatataskQueryForm;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.form.DatataskUpdateForm;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.dto.DataSourceDTO;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.vo.DatataskVO;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.vo.StatusVO;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.service.DatataskService;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ValidateList;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 任务配置 Controller
 *
 * @Author KeissCake
 * @Date 2024-09-26 21:17:52
 * @Copyright 暂无
 */

@RestController
@Tag(name = "任务配置")
public class DatataskController {

    @Resource
    private DatataskService datataskService;

    @Operation(summary = "分页查询 @author KeissCake")
    @PostMapping("/datatask/queryPage")
    @SaCheckPermission("datatask:query")
    public ResponseDTO<PageResult<DatataskVO>> queryPage(@RequestBody @Valid DatataskQueryForm queryForm) {
        return ResponseDTO.ok(datataskService.queryPage(queryForm));
    }

    @Operation(summary = "添加 @author KeissCake")
    @PostMapping("/datatask/add")
    @SaCheckPermission("datatask:add")
    public ResponseDTO<String> add(@RequestBody @Valid DatataskAddForm addForm) {
        return datataskService.add(addForm);
    }

    @Operation(summary = "更新 @author KeissCake")
    @PostMapping("/datatask/update")
    @SaCheckPermission("datatask:update")
    public ResponseDTO<String> update(@RequestBody @Valid DatataskUpdateForm updateForm) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        updateForm.setUpdateUser(requestUser.getUserName());
        return datataskService.update(updateForm);
    }

    @Operation(summary = "批量删除 @author KeissCake")
    @PostMapping("/datatask/batchDelete")
    @SaCheckPermission("datatask:delete")
    public ResponseDTO<String> batchDelete(@RequestBody ValidateList<Integer> idList) {
        return datataskService.batchDelete(idList);
    }

    @Operation(summary = "单个删除 @author KeissCake")
    @GetMapping("/datatask/delete/{taskId}")
    @SaCheckPermission("datatask:delete")
    public ResponseDTO<String> batchDelete(@PathVariable Integer taskId) {
        return datataskService.delete(taskId);
    }

    @Operation(summary = "更新启用状态")
    @PostMapping("/datatask/updateStatus")
    @SaCheckPermission("datatask:update")
    public ResponseDTO<String> updateStatus(@RequestBody StatusDTO statusDTO) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        return datataskService.updateStatus(statusDTO.getTaskId(), statusDTO.getStatus(),requestUser.getUserName());
    }
    @Operation(summary = "获取状态")
    @GetMapping("/datatask/queryStatus")
    @SaCheckPermission("datatask:query")
    public ResponseDTO<StatusVO> queryStatus(@RequestParam Integer taskId) {
        return ResponseDTO.ok(datataskService.queryStatus(taskId));
    }
    @Operation(summary = "通过DataSource新建任务")
    @PostMapping("/datatask/addTask")
    @SaCheckPermission("datatask:add")
    public ResponseDTO<String> addTask(@RequestBody DataSourceDTO dataSourceId) {
        System.out.println("这是获取到的主键:"+dataSourceId.getDataSourceId());
        return datataskService.addTask(dataSourceId.getDataSourceId());
    }
    @Operation(summary = "获取DataSource的类型")
    @GetMapping("/datatask/queryDataSourceType")
    @SaCheckPermission("datatask:query")
    public ResponseDTO<String> queryDataSourceType(@RequestParam Integer dataSourceId) {
        return ResponseDTO.ok(datataskService.queryDataSourceType(dataSourceId));
    }
}
